//
//  XVCReleaseProduct.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/1.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCReleaseProduct.h"
#import "XVVSelectProductImg.h"
#import "XXTool.h"
#import "XVVTextViewWithPlaceholder.h"
#import "XMLSendProductModel.h"

@interface XVCReleaseProduct ()<UITextViewDelegate>

@property (nonatomic,strong) UIScrollView *content;

@property (nonatomic,strong) XVVSelectProductImg* selectImgView;

@property (nonatomic,strong) UITextField *inputProductName;

@property (nonatomic,strong) XVVTextViewWithPlaceholder *inputProductDescribe;

@end

@implementation XVCReleaseProduct

-(UIScrollView*)content
{
    if (_content==nil)
    {
        _content = [[UIScrollView alloc] init];
        _content.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_content];
    }
    return _content;
}

-(XVVSelectProductImg*)selectImgView
{
    if (_selectImgView==nil)
    {
        _selectImgView = [[XVVSelectProductImg alloc] init];
        [_content addSubview:_selectImgView];
    }
    return _selectImgView;
}

-(UITextField*)inputProductName
{
    if (_inputProductName==nil)
    {
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 5)];
        _inputProductName = [[UITextField alloc] init];
        _inputProductName.placeholder = NSLocalizedString(@"请输入产品名称", nil);
        _inputProductName.backgroundColor = [XXTool colorWithBackGround];
        _inputProductName.leftView = leftView;
        _inputProductName.leftViewMode = UITextFieldViewModeAlways;
        _inputProductName.font = [XXTool systemFontOfSize:16];
        [_inputProductName setValue:[XXTool colorWithHexString:@"8B8B8B"]
                             forKeyPath:@"_placeholderLabel.textColor"];
        _inputProductName.returnKeyType = UIReturnKeyDone;
        [_inputProductName addTarget:self action:@selector(clickTextFieldDone) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_content addSubview:_inputProductName];
    }
    return _inputProductName;
}

-(XVVTextViewWithPlaceholder*)inputProductDescribe
{
    if (_inputProductDescribe==nil)
    {
        _inputProductDescribe = [[XVVTextViewWithPlaceholder alloc] init];
        _inputProductDescribe.textFont = [XXTool systemFontOfSize:16];
        _inputProductDescribe.placeholder_textColor = [XXTool colorWithHexString:@"8B8B8B"];
        _inputProductDescribe.placeholder = NSLocalizedString(@"请输入300字以内的产品描述", nil);
        _inputProductDescribe.backgroundColor =[XXTool colorWithBackGround];
        [_content addSubview:_inputProductDescribe];
        
    }
    return _inputProductDescribe;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:NSLocalizedString(@"发布展品", nil)];
    [self content];
    [self selectImgView];
    [self inputProductName];
    [self inputProductDescribe];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"发布", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickReleaseProduct)]];
}

-(void)initData
{
    [SVProgressHUD show];
    [XMLSendProductModel requsetLastRelaseInformationWithCompletion:^(BOOL isSuccess, XMLSendProductModel *model) {
        if (isSuccess)
        {
            _inputProductName.text = model.goods_name;
            [_inputProductDescribe setinputTextString:model.goods_desc];
            [_selectImgView setRequestSelectedImg:model.list];
            [SVProgressHUD dismiss];
        }
    }];
}

-(void)refreshLayout:(CGRect)frame
{
    _content.frame = CGRectMake(0, 0, frame.size.width
                                , frame.size.height);
    _selectImgView.frame = CGRectMake(0, 0, frame.size.width
                                      , [XVVSelectProductImg getHeight]);
    _inputProductName.frame = CGRectMake(0, _selectImgView.frame.size.height+_selectImgView.frame.origin.y
                                         , frame.size.width
                                         , 54);
    _inputProductDescribe.frame = CGRectMake(0
                                             , _inputProductName.frame.size.height+_inputProductName.frame.origin.y+20
                                             , frame.size.width
                                             , 350);
    _content.contentSize = CGSizeMake(frame.size.width
                                      , _inputProductDescribe.frame.size.height+_inputProductDescribe.frame.origin.y);
}

-(void)clickTextFieldDone
{
    [_inputProductName resignFirstResponder];
    [_inputProductDescribe.inputView resignFirstResponder];
}

-(void)clickReleaseProduct
{
    if ([_inputProductName.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"请输入展品名称", nil)];
        return;
    }
    if ([[_inputProductDescribe inputTextString] isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"请输入展品描述", nil)];
        return;
    }
    if ([_inputProductDescribe inputTextString].length>300)
    {
        [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"展品描述必须小于300字符", nil)];
        return;
    }
    [SVProgressHUD show];
    XMLSendProductModel *model = [[XMLSendProductModel alloc] init];
    model.goods_name = _inputProductName.text;
    model.goods_desc = [_inputProductDescribe inputTextString];
    model.list = [_selectImgView getSelectProductImgArray];
    [XMLSendProductModel requestPeleaseGoodsWithreleaseInformation:model completion:^(BOOL isSuccess) {
        if (isSuccess)
        {
            [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"发布成功", nil)];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
